<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapperPkg}.${entityName}Mapper">

    <resultMap id="BaseResultMap" type="${entityPkg}.${entityName}">
        <#list columnEntityList as filed>
            <#if primaryKey!='noPrimaryKey'&& filed.sqlField==primaryKey>
                <id column="${filed.sqlField}" jdbcType="${filed.columnJdbcType}" property="id" />
            <#else>
                <result column="${filed.sqlField}" jdbcType="${filed.columnJdbcType}" property="${filed.fieldNameLow}" />
            </#if>
        </#list>
    </resultMap>


    <sql id="Example_Where_Clause" >
        <where >
            <foreach collection="oredCriteria" item="criteria" separator="or" >
                <if test="criteria.valid" >
                    <trim prefix="(" suffix=")" prefixOverrides="and" >
                        <foreach collection="criteria.criteria" item="criterion" >
                            <choose >
                                <when test="criterion.noValue" >
                                    and ${r'$'}{criterion.condition}
                                </when>
                                <when test="criterion.singleValue" >
                                    and ${r'$'}{criterion.condition}  ${r'#'}{criterion.value}
                                </when>
                                <when test="criterion.betweenValue" >
                                    and ${r'$'}{criterion.condition}  ${r'#'}{criterion.value} and  ${r'#'}{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue" >
                                    and ${r'$'}{criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                                        ${r'#'}{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>

    <sql id="Update_By_Example_Where_Clause" >
        <where >
            <foreach collection="example.oredCriteria" item="criteria" separator="or" >
                <if test="criteria.valid" >
                    <trim prefix="(" suffix=")" prefixOverrides="and" >
                        <foreach collection="criteria.criteria" item="criterion" >
                            <choose >
                                <when test="criterion.noValue" >
                                    and ${r'$'}{criterion.condition}
                                </when>
                                <when test="criterion.singleValue" >
                                    and ${r'$'}{criterion.condition}  ${r'#'}{criterion.value}
                                </when>
                                <when test="criterion.betweenValue" >
                                    and ${r'$'}{criterion.condition}  ${r'#'}{criterion.value} and  ${r'#'}{criterion.secondValue}
                                </when>
                                <when test="criterion.listValue" >
                                    and ${r'$'}{criterion.condition}
                                    <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                                        ${r'#'}{listItem}
                                    </foreach>
                                </when>
                            </choose>
                        </foreach>
                    </trim>
                </if>
            </foreach>
        </where>
    </sql>

    <sql id="Base_Column_List" >
        <#list columnEntityList as filed>${filed.sqlField}<#sep>,</#sep></#list>
    </sql>

    <select id="selectByExample" resultMap="BaseResultMap" parameterType="${entityPkg}.${entityName}Criteria" >
        select
        <if test="distinct" >
            distinct
        </if>
        'true' as QUERYID,
        <include refid="Base_Column_List" />
        from ${prefix}${tableName}
        <if test="_parameter != null" >
            <include refid="Example_Where_Clause" />
        </if>
        <if test="orderByClause != null" >
            order by ${r'$'}{orderByClause}
        </if>
    </select>

    <select id="selectByUniqCond" resultMap="BaseResultMap" parameterType="${entityPkg}.${entityName}Criteria" >
        select
        <if test="distinct" >
            distinct
        </if>
        'true' as QUERYID,
        <include refid="Base_Column_List" />
        from ${prefix}${tableName}
        <if test="_parameter != null" >
            <include refid="Example_Where_Clause" />
        </if>
    </select>

    <#if primaryKey!='noPrimaryKey'>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
        select
        <include refid="Base_Column_List" />
        from ${prefix}${tableName}
        where ${primaryKey} = ${r'#'}{${javaKey}}
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from ${prefix}${tableName}
        where ${primaryKey} = ${r'#'}{${javaKey}}
    </delete>
    </#if>


    <delete id="deleteByExample" parameterType="${entityPkg}.${entityName}Criteria" >
        delete from ${prefix}${tableName}
        <if test="_parameter != null" >
            <include refid="Example_Where_Clause" />
        </if>
    </delete>

    <select id="countByExample" parameterType="${entityPkg}.${entityName}Criteria" resultType="java.lang.Integer" >
        select count(*) from ${prefix}${tableName}
        <if test="_parameter != null" >
            <include refid="Example_Where_Clause" />
        </if>
    </select>
    <!-- 插入后返回主键-->
    <insert id="insertSelective" parameterType="${entityPkg}.${entityName}" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into ${prefix}${tableName}
        <trim prefix="(" suffix=")" suffixOverrides="," >
            <#list columnEntityList as filed>
                <#if !filed.extra?contains("auto")>
                    <#if filed.sqlFieldDataType?index_of("char")!=-1 >
                        <if test="${filed.fieldNameLow}!= null and ${filed.fieldNameLow}!='' ">
                            ${filed.sqlField}<#sep>,</#sep>
                        </if>
                    <#else>
                        <if test="${filed.fieldNameLow}!= null">
                            ${filed.sqlField}<#sep>,</#sep>
                        </if>
                    </#if>
                </#if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            <#list columnEntityList as filed>
                <#if !filed.extra?contains("auto")>
                    <#if filed.sqlFieldDataType?index_of("char")!=-1 >
                        <if test="${filed.fieldNameLow}!= null and ${filed.fieldNameLow}!='' ">
                            ${r'#'}{${filed.fieldNameLow}}<#sep>,</#sep>
                        </if>
                    <#else>
                        <if test="${filed.fieldNameLow}!= null">
                            ${r'#'}{${filed.fieldNameLow}}<#sep>,</#sep>
                        </if>
                    </#if>
                </#if>
            </#list>
        </trim>
    </insert>

    <!-- 插入后返回主键-->
    <insert id="insert" parameterType="${entityPkg}.${entityName}" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into ${prefix}${tableName}
        (
        <#list columnEntityList as filed>
            ${filed.sqlField}<#sep>,</#sep>
        </#list>
        )
        values
        (
        <#list columnEntityList as filed>
            ${r'#'}{${filed.fieldNameLow}}<#sep>,</#sep>
        </#list>
        )
    </insert>

    <update id="updateByExample" parameterType="map" >
        update ${prefix}${tableName}
        <set>
            <#list columnEntityList as filed>
                <#if filed.sqlField!=primaryKey>
                    ${filed.sqlField} = ${r'#'}{record.${filed.fieldNameLow}},
                </#if>
            </#list>
        </set>
        <if test="_parameter != null" >
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>

    <update id="updateByExampleSelective" parameterType="map">
        update ${prefix}${tableName}
        <set>
            <#list columnEntityList as filed>
                <#if filed.sqlField!=primaryKey>
                    <#if filed.sqlFieldDataType?index_of("char")!=-1 >
                        <if test="record.${filed.fieldNameLow}!= null and record.${filed.fieldNameLow}!='' ">
                            ${filed.sqlField} = ${r'#'}{record.${filed.fieldNameLow}},
                        </if>
                    <#else>
                        <if test="record.${filed.fieldNameLow}!= null">
                            ${filed.sqlField} = ${r'#'}{record.${filed.fieldNameLow}},
                        </if>
                    </#if>
                </#if>
            </#list>
        </set>
        <if test="_parameter != null" >
            <include refid="Update_By_Example_Where_Clause" />
        </if>
    </update>
    <#if primaryKey!='noPrimaryKey'>
    <update id="updateByPrimaryKeySelective" parameterType="${entityPkg}.${entityName}">
        update ${prefix}${tableName}
        <set>
            <#list columnEntityList as filed>
                <#if filed.sqlField!=primaryKey>
                    <#if filed.sqlFieldDataType?index_of("char")!=-1 >
                        <if test="${filed.fieldNameLow}!= null and ${filed.fieldNameLow}!='' ">
                            ${filed.sqlField} = ${r'#'}{${filed.fieldNameLow}},
                        </if>
                    <#else>
                        <if test="${filed.fieldNameLow}!= null">
                            ${filed.sqlField} = ${r'#'}{${filed.fieldNameLow}},
                        </if>
                    </#if>
                </#if>
            </#list>
        </set>
        where ${primaryKey} = ${r'#'}{${javaKey}}
    </update>

    <update id="updateByPrimaryKey" parameterType="${entityPkg}.${entityName}">
        update ${prefix}${tableName}
        <set>
            <#list columnEntityList as filed>
                <#if filed.sqlField!=primaryKey>
                    ${filed.sqlField} = ${r'#'}{${filed.fieldNameLow}},
                </#if>
            </#list>
        </set>
        where ${primaryKey} = ${r'#'}{${javaKey}}
    </update>
    </#if>

    <insert id="batchInsert" parameterType="java.util.List" >
        insert into ${prefix}${tableName}
        <@compress single_line=true>(
            <#list columnEntityList as filed>
                <#if !filed.extra?contains("auto")>
                    ${filed.sqlField}<#sep>,</#sep>
                </#if>
            </#list>
            )
        </@compress>
        values
        <foreach collection="list" index="index" item="item" separator=",">
            <@compress single_line=true>
                (
                <#list columnEntityList as filed>
                    <#if !filed.extra?contains("auto")>
                        ${r'#'}{item.${filed.fieldNameLow}}<#sep>,</#sep>
                    </#if>
                </#list>
                )
            </@compress>
        </foreach>
    </insert>

    <#if primaryKey!='noPrimaryKey'>
    <update id="batchUpdate" parameterType="java.util.List">
        update ${prefix}${tableName}
        <trim prefix="set" suffixOverrides=",">
            <foreach collection="list" index="index" item="item" separator=",">
                <@compress single_line=true>
                    (
                    <#list columnEntityList as filed>
                        <#if !filed.extra?contains("auto")>
                            <trim prefix="${filed.sqlField} =case" suffix="end,">
                                <foreach collection="list" item="item" index="index">
                                    <#if filed.sqlFieldDataType?index_of("char")!=-1>
                                        <if test="${filed.fieldNameLow}!= null and ${filed.fieldNameLow}!=''">
                                            when id=${r'#'}{item.${primaryKey}}
                                            then ${r'#'}{item.${filed.fieldNameLow}}
                                        </if>
                                    <#else>
                                        <if test="${filed.fieldNameLow}!= null">
                                            when id=${r'#'}{item.${primaryKey}}
                                            then ${r'#'}{item.${filed.fieldNameLow}}
                                        </if>
                                    </#if>
                                </foreach>
                            </trim>
                        </#if>
                    </#list>
                    )
                </@compress>
            </foreach>
        </trim>
        where  ${primaryKey}  in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            ${r'#'}{item.${primaryKey}}
        </foreach>
    </update>
    </#if>
</mapper>